<template>
    <div id='' class='tenantrole_content'>
        <i-table class="table_content" ref="myTable" :search="getSearch" :table="getTbale" :pageConfig="pageConfig" />
        <Modal v-model="modalShow" width="1200" :mask-closable="false" :title="modalTitle">
            <components v-if="modalShow" :is="comps" ref="coms" />
            <div slot="footer">
                <Button type="text" @click="cancel">取消</Button>
                <Button type="primary" @click="ok">确定</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
import { config } from './role/table_config'
import AddOrUpdata from './role/add_or_updata.vue'
export default {
    name: '',
    props: [''],
    data() {
        return {
            modalShow: false,
            modalTitle: '新增',
            search: {}, // 查询配置对象
            tableConfig: {}, // 表格配置对象
            pageConfig: config.page, // 翻页器配置
            comps: AddOrUpdata
        };
    },
    components: {},
    computed: {
        getSearch() {
            this.search = icu.util.deepClone(config.search);// 为不改变原始配置数据，进行深度克隆。
            return this.search;
        },
        getTbale() {
            this.tableConfig = icu.util.deepClone(config.table); // 深度克隆表格配置。
            this.tableConfig.event = {
                edit: this.edit,
                add: this.add,
                remove: this.remove
            }
            return this.tableConfig;
        }
    },
    beforeMount() { },
    mounted() {
        this['$api'] = this.$Api.get(['systemSetup:systemManger']);
    },
    methods: {
        add() {
            this.modalTitle = '新增';
            this.modalShow = true;
            this.getAuthenMenuTree();
        },
        edit(event, row) {
            this.modalTitle = '编辑';
            this.modalShow = true;
            this.getAuthenMenuTree((treeData) => {
                this.$http.$get('roleDetail', { params: { id: row.id } }).then(res => {
                    if (res.code == 200) {
                        this.setCheckLists(res.data, treeData.authen);
                        this.setCheckLists(res.data, treeData.function);
                        this.$refs.coms.setData(res.data, treeData);
                        return
                    }
                    this.$Message.error(res.msg)
                })
            });
        },

        // 设置勾选
        setCheckLists(data, treeList) {
            treeList.forEach(item => {
                if (item.children && item.children.length > 0) {
                    this.setCheckLists(data, item.children)
                } else {
                    if (data.menuIds && data.menuIds.includes(item.id)) {
                        item.checked = true;
                    }
                }
            })
        },

        // 获取角色菜单树
        getAuthenMenuTree(callback) {
            this.$api.roleAuthenMenuTree({ systemType: '' }, data => {
                if (callback) {
                    callback(data);
                    return
                }
                this.$refs.coms.setTreeList(data);
            })
        },

        getList() {
            this.$refs.myTable.seach();
        },

        ok() {
            this.$refs.coms.getForm((formRes) => {
                let menus = this.$refs.coms.getTreeCheck();
                this.$http.$post('tenantRoleSaveTenantRole', { data: { ...formRes, ... { menus } } }).then(res => {
                    if (res.code == 200) {
                        this.$Message.success('保存成功');
                        this.cancel();
                        this.getList();
                        return
                    }
                    this.$Message.error(res.msg)
                })
            })
        },

        cancel() {
            this.modalShow = false
        },

        remove(item, row) {
            this.$Modal.confirm({
                title: '删除确认',
                content: '确认删除该条数据？',
                onOk: () => {
                    this.$http.$post('roleDelete', { data: [row.id] }).then(res => {
                        if (res.code == 200) {
                            this.$Message.success('删除成功');
                            this.getList()
                            return
                        }
                        this.$Message.error(res.msg);
                    })
                },
            });
        },
    },
    watch: {}
}
</script>
<style lang='less' scoped>
.tenantrole_content {
    background-color: #fff;
    padding: 0 20px;
    width: 100%;
    height: 800px;
    .table_content {
        height: 752px;
        overflow: auto;
    }
}
</style>